package yt.kpool.adapter;

import android.app.Activity;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import yt.kpool.R;
import yt.kpool.activity.miner.MinerChartActivity;
import yt.kpool.bean.MinerBean;
import yt.kpool.util.TimeUtil;

/**
 * 矿工列表
 * @Author:Created by  ygy
 * @time:2019/10/8
 */
public class MinerListAdapter extends ViewHolderRecyclerAdapter<MinerBean.DataBean.MinerListBean> {

    private int type;//0带拒绝率 1带最近提交时间
    private int childaccountId;//子账户id
    private int currencyID;//币种id

    private boolean isIsFastLogin=false;
    private  List<MinerBean.DataBean.MinerListBean> listData;
    // 存储勾选框状态的map集合

    private Map<Integer,Boolean> isCheck = new HashMap();
    public int getChildaccountId() {
        return childaccountId;
    }

    public void setChildaccountId(int childaccountId) {
        this.childaccountId = childaccountId;
    }

    public int getCurrencyID() {
        return currencyID;
    }

    public void setCurrencyID(int currencyID) {
        this.currencyID = currencyID;
    }

    public boolean isIsFastLogin() {
        return isIsFastLogin;
    }

    public void setIsFastLogin(boolean isFastLogin) {
        isIsFastLogin = isFastLogin;
    }

    private OnCheckBoxListener onCheckBoxListener;

    public Map<Integer, Boolean> getIsCheck() {
        return isCheck;
    }

    public void setIsCheck(Map<Integer, Boolean> isCheck) {
        this.isCheck = isCheck;
    }

    public MinerListAdapter(Context context, List<MinerBean.DataBean.MinerListBean> listData, OnCheckBoxListener onCheckBoxListener) {
        super(context, listData);
        this.listData=listData;
        this.onCheckBoxListener=onCheckBoxListener;
       initCheck(false);
    }

    /**
     * 初始化map集合
     * @param checked
     */
    public void initCheck(boolean checked) {
        for (int i = 0; i < listData.size(); i++) {
            isCheck.put(i,checked);
        }
    }


    @Override
    public View buildConvertView(LayoutInflater layoutInflater, int viewType) {
        return layoutInflater.inflate(R.layout.item_minerlist,null);
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void bindViewDatas(final ViewHolder holder, final MinerBean.DataBean.MinerListBean minerListBean, final int position) {

        holder.setText(R.id.tv_name, minerListBean.getName());
        String[] s = minerListBean.getHashrate().split(" ");
        if (s.length == 2) {
            holder.setText(R.id.tv_hashrate, s[0]);
            holder.setText(R.id.tv_hashrateunit, s[1]);
        }
        String[] s2 = minerListBean.getHashrate24H().split(" ");
        if (s2.length == 2) {
            holder.setText(R.id.tv_hashrate24H, s2[0]);
            holder.setText(R.id.tv_hashrate24Hunit, s2[1]);
        }


        if (type == 0) {
            NumberFormat nf = NumberFormat.getPercentInstance();
            nf.setMaximumFractionDigits(1);
//            holder.setText(R.id.tv_reject,nf.format(minerListBean.getReject()));
            holder.setText(R.id.tv_reject, new DecimalFormat("0.00").format(100*minerListBean.getReject())+ "%");
        } else {
            holder.setText(R.id.tv_reject, TimeUtil.dataString2String(minerListBean.getLastShare()));
        }

        //列表单条点击
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MinerChartActivity.start((Activity) context, "" + childaccountId, "" + getCurrencyID(), "" + minerListBean.getName(), minerListBean.getHashrate(), minerListBean.getHashrate24H(), minerListBean.getLastShare(), minerListBean.getReject());


            }
        });

        if (isIsFastLogin())
        {
            //快速登录
            holder.getView(R.id.checkbox).setVisibility(View.GONE);
        }else {
            holder.getView(R.id.checkbox).setVisibility(View.VISIBLE);
        }
      //  holder.getView(R.id.checkbox).setTag(position);
        ((CheckBox)holder.getView(R.id.checkbox)).setOnCheckedChangeListener(null);
        if (isCheck.get(position) != null)
        {
            ((CheckBox)holder.getView(R.id.checkbox)).setChecked(isCheck.get(position));
        }

        ((CheckBox)holder.getView(R.id.checkbox)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isCheck.put(position,b);


                    onCheckBoxListener.clickCheck();

            }
        });
    }

    /**
     * 加载更多更新数据
     */
    public  void moreMinerNotifyDataSetChanged()
    {

        for (int i = 0; i <listData.size(); i++) {
            if (isCheck.get(i)==null)
            {
                isCheck.put(i,false);
            }
        }
        notifyDataSetChanged();
    }
    /**
     * 刷新更新数据
     */
    public  void refrashMinerNotifyDataSetChanged()
    {
        isCheck.clear();
        for (int i = 0; i <listData.size(); i++) {

                isCheck.put(i,false);

        }
        notifyDataSetChanged();
    }

    /**
     * 点击回调
     */
    public interface OnCheckBoxListener {
        public void clickCheck();

    }
}
